package com.situ.studentmanagement.controller;

import com.octo.captcha.Captcha;
import com.situ.studentmanagement.dao.AfficheMapper;
import com.situ.studentmanagement.dao.GoodsMapper;
import com.situ.studentmanagement.dao.UserMapper;
import com.situ.studentmanagement.model.Affiche;
import com.situ.studentmanagement.model.Goods;
import com.situ.studentmanagement.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

import java.util.List;

import static com.situ.studentmanagement.Constants.USER_ID;
import static com.situ.studentmanagement.Constants.USER_NAME;

/**
 * Created by baba on 2016/8/2.
 */
@Controller //表示这个类是一个Controller，于是当请求路径 / 时，这个Contrller的实例被自动创建
@RequestMapping("/user") //这个controller对应到路径 /
public class user {
    @Autowired
    public UserMapper userMapper;
    @Autowired
    AfficheMapper afficheMapper;
    @Autowired
    GoodsMapper goodsMapper;
    private String userprotect="";

    @RequestMapping("/register")
    public String register(Model model, HttpSession session,
                           @RequestParam("name")String name,
                           @RequestParam("realname")String realname,
                           @RequestParam("password") String password,
                           @RequestParam("passwordtwo") String passwordtwo,
                           @RequestParam("protect") String protect,
                           @RequestParam("answer") String answer,
                           @RequestParam("phone") String phone,
                           @RequestParam("address") String address,
                           @RequestParam("sex") String sex,
                           @RequestParam("random") String random){

        Integer StudentSex=1;
        String protectstring="";
        String errMsg=null;
        Captcha captcha= (Captcha) session.getAttribute("captcha");
        if(!captcha.validateResponse(random)) {
            //验证码错误！
            errMsg = "验证码输的不对呀!";
            model.addAttribute("errMsg", errMsg);
            return "register"; //返回前台文件件的名字。
        }

        if(password.equals(passwordtwo)){
            if(sex.equals("man")){
                StudentSex=1;
            }else {
                StudentSex=0;
            }
            int protectint=Integer.parseInt(protect);
            switch (protectint){
                case 1:
                    protectstring="您父亲的名字";
                    break;
                case 2:
                    protectstring="您母亲的名字";
                    break;
                case 3:
                    protectstring="您的小学学校的名字";
                    break;
                case 4:
                    protectstring="您初中学校的名字";
                    break;
                case 5:
                    protectstring="您最好的朋友的名字";
                    break;
            }
            User user=new User(0,name,realname,password,protectstring,answer,StudentSex,phone,address);
            userMapper.insertUser(user);
            int stuID=user.getId();
            session.setAttribute(USER_ID,Integer.toString(stuID));
            model.addAttribute("value_name",name);
        }else {
            model.addAttribute("errMsg","两次输入的密码不一致，请重新输入");
            return "register";
        }
        return "redirect:/";
    }

    @RequestMapping("/backtoleft")
    public String backtoleft(Model model, HttpSession session){

            return "left";
        }
    @RequestMapping("/gotofindpassword")
    public String gotofindpassword(Model model, HttpSession session){

        return "findpassword";
    }

    @RequestMapping("/findpassword")
    public String register(Model model, HttpSession session,
                           @RequestParam("name")String name,
                           @RequestParam("random")String random){
        String errMsg="";
        Captcha captcha= (Captcha) session.getAttribute("captcha");
        User user=userMapper.selectUserByName(name);
        if(user==null){
            errMsg="您输入的用户名有误，请重新输入！";
            return "findpassword";
        }
        if(!captcha.validateResponse(random)) {
            //验证码错误！
            errMsg = "验证码输的不对呀!";
            model.addAttribute("errMsg", errMsg);
            return "findpassword"; //返回前台文件件的名字。
        }else{
            session.setAttribute(USER_ID,user.getId());
            userprotect=user.getAnswer();
            model.addAttribute("protect",user.getProtect());
            return "writeprotect";
        }
    }

    @RequestMapping("/modifypassword")
    public String modifypassword(Model model, HttpSession session,
                           @RequestParam("answer")String answer,
                           @RequestParam("password")String password){
        String errMsg="";
        if(!userprotect.equals(answer)){
            errMsg="您输入的密保问题错误！请重新输入";
            model.addAttribute("errMsg", errMsg);
            return "writeprotect";
        }else {
            int id=Integer.parseInt(session.getAttribute(USER_ID).toString());
            userMapper.setpassword(id,password);
            return "left";
        }
    }

    @RequestMapping("/gotomodify")
    public String gotomodify(Model model, HttpSession session) {
        String errMsg = "";
        if (session.getAttribute(USER_ID) != null) {
            model.addAttribute("name", session.getAttribute(USER_NAME));
            model.addAttribute("message", 1);
            User user = userMapper.selectUserByName(session.getAttribute(USER_NAME).toString());
            model.addAttribute("protect", user.getProtect());
        }else {

        }
        List<Affiche> affiches = afficheMapper.selectAllAffiche();
        model.addAttribute("affiches", affiches);
        return "usermodify";
    }


    @RequestMapping("/checkprotect")
    public String checkprotect(Model model, HttpSession session,
                                 @RequestParam("answer")String answer,
                               @RequestParam("random")String random){
        String errMsg="";
        Captcha captcha= (Captcha) session.getAttribute("captcha");

        if(!captcha.validateResponse(random)) {
            //验证码错误！
            model.addAttribute("message", 1);
            User user = userMapper.selectUserByName(session.getAttribute(USER_NAME).toString());
            List<Affiche> affiches = afficheMapper.selectAllAffiche();
            model.addAttribute("affiches", affiches);
            model.addAttribute("protect", user.getProtect());
            model.addAttribute("name", session.getAttribute(USER_NAME));
            errMsg = "验证码输的不对呀!";
            model.addAttribute("errMsg", errMsg);
            return "usermodify"; //返回前台文件件的名字。
        }else{
            model.addAttribute("message", 1);
            List<Affiche> affiches = afficheMapper.selectAllAffiche();
            model.addAttribute("affiches", affiches);
            model.addAttribute("affiches", affiches);
            model.addAttribute("name", session.getAttribute(USER_NAME));
            User user = userMapper.selectUserByName(session.getAttribute(USER_NAME).toString());
            model.addAttribute("user",user);
            return "setusermessage";
        }
    }

    @RequestMapping("/finalchange")
    public String finalchange(Model model, HttpSession session,
                               @RequestParam("name")String name,
                               @RequestParam("phone")String phone,
                              @RequestParam("address")String address){
        model.addAttribute("message", 1);
        int id=Integer.parseInt(session.getAttribute(USER_ID).toString());
        userMapper.changeusermessage(id,name,phone,address);
        List<Goods> goodses=goodsMapper.selectAllGoods();
        model.addAttribute("goods",goodses);
        List<Affiche> affiches = afficheMapper.selectAllAffiche();
        model.addAttribute("affiches", affiches);
        model.addAttribute("name", session.getAttribute(USER_NAME));
        return "index";
    }

}
